package com.stylefeng.guns.modular.goods.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.goods.model.dto.SpuDto;
import com.stylefeng.guns.modular.goods.service.IBrandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.goods.model.Spu;
import com.stylefeng.guns.modular.goods.service.ISpuService;

import java.util.Map;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-09-05 16:10:22
 */
@Controller
@RequestMapping("/spu")
public class SpuController extends BaseController {

    private String PREFIX = "/goods/spu/";

    @Autowired
    private ISpuService spuService;

    @Autowired
    private IBrandService brandService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "spu.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/spu_add")
    public String spuAdd() {
        return PREFIX + "spu_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/spu_update/{spuId}")
    public String spuUpdate(@PathVariable Long spuId, Model model) {
        Spu spu = spuService.selectById(spuId);
        model.addAttribute("item",spu);
        LogObjectHolder.me().set(spu);
        return PREFIX + "spu_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return spuService.selectList(null);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SpuDto spuDto) {
        spuService.save(spuDto);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long spuId) {
        spuService.deleteAll(spuId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Spu spu) {
        spuService.updateById(spu);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{spuId}")
    @ResponseBody
    public Object detail(@PathVariable("spuId") Long spuId) {
        return spuService.selectById(spuId);
    }

}
